package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 89311 on 2018/4/25.
 */

public class ElementAdapter extends RecyclerView.Adapter<ElementAdapter.LinerViewHolder> {
    private Context context;
    private JSONArray jsonArray;
    private RecyclerView recyclerView;
    public int a = 0;
    private ElementActivity elementActivity;
    private String bank_name;

    public ElementAdapter(Context context, JSONArray jsonArray,RecyclerView recyclerView,ElementActivity elementActivity) {
        this.context = context;
        this.jsonArray = jsonArray;
        this.recyclerView = recyclerView;
        this.elementActivity = elementActivity;
    }


    @Override
    public ElementAdapter.LinerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ElementAdapter.LinerViewHolder(LayoutInflater.from(context).inflate(R.layout.wz_element_item_view,parent,false));
    }

    @Override
    public void onBindViewHolder(final ElementAdapter.LinerViewHolder holder, final int position) {
        try {
            JSONObject jsb  = (JSONObject) jsonArray.get(position);
            bank_name = jsb.get("bankcode").toString();
            if(position==0){
                elementActivity.setA(0);
                holder.ElementViewImage.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.img_0004));
            }

            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    holder.ElementViewImage.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.img_0004));
                    if(a!=position) {

                            ElementAdapter.LinerViewHolder holder1 = (LinerViewHolder) recyclerView.findViewHolderForLayoutPosition(a);
                            holder1.ElementViewImage.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.img_0005));

                    }
                    a = position;
                    elementActivity.setA(a);
                    showToast((Activity) context,a+"");
                }
            });

            //所属银行
            holder.ElementBankName.setText(jsb.get("bankname").toString());
            //替换字符
            String cardId = jsb.get("cardnumber").toString();
            int length=cardId.length();
            String str=cardId.substring(0, length-11)+"*******"+cardId.substring(length-2);
            //银行卡号
            holder.ElementCardId.setText(str);
            //银行图标
            String new_bank_name = bank_name.toLowerCase();
            Bitmap bitmap = getBitmapByName("bank_"+new_bank_name);
            holder.ElementViewCard.setImageBitmap(bitmap);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        if(jsonArray!=null){
            return jsonArray.length();
        }else{
            return 0;
        }
    }

    class LinerViewHolder extends RecyclerView.ViewHolder{
        private TextView ElementBankName,ElementCardId;
        private ImageButton ElementViewImage;
        private ImageView ElementViewCard;
        private View view;
        public LinerViewHolder(View itemView) {
            super(itemView);
            ElementBankName = itemView.findViewById(R.id.ElementBankName);
            ElementCardId = itemView.findViewById(R.id.ElementCardId);
            ElementViewImage = itemView.findViewById(R.id.ElementViewImage);
            ElementViewCard = itemView.findViewById(R.id.ElementViewCard);
            view = itemView;
        }
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public Bitmap getBitmapByName(String name) {
        ApplicationInfo appInfo = context.getApplicationInfo();
        int resID = context.getResources().getIdentifier(name, "mipmap", appInfo.packageName);
        return BitmapFactory.decodeResource(context.getResources(), resID);
    }
}
